﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Maps.MapControl;
using System.Globalization;
using Microsoft.Maps.MapControl.Design;
using System.Collections.ObjectModel;

using System.ServiceModel;

namespace MyPCTagging
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        private void ChangeMapView_Click(object sender, RoutedEventArgs e)
        {
            this.viewMap.Mode = new AerialMode(false);
            TreeView myTree = new TreeView();     
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadAllDevicesInfo();
        }

        /*
         * Encapsulate WCF service operation "GetAllDeviceInfo"
         */
        public void LoadAllDevicesInfo()
        {
            EndpointAddress address = new EndpointAddress(new Uri(Application.Current.Host.Source, "/DatabaseService.svc")); 
            MyDBService.DatabaseServiceClient dbsClient = new MyDBService.DatabaseServiceClient(new BasicHttpBinding(), address);
            dbsClient.GetAllDevicesInfoCompleted += LoadAllDevicesInfoCompleted;
            dbsClient.GetAllDevicesInfoAsync();
        }

        /*
         * Callback function: load all devices information in TreeView
         */ 
        void LoadAllDevicesInfoCompleted(object sender, MyDBService.GetAllDevicesInfoCompletedEventArgs e)
        {
            Node firstNode = new Node();
            Node rootNode = new Node();
            rootNode.NodeName = (e.Error == null) ? e.Result.name : e.Error.ToString();
            LoadAllDevicesInfoHelper(ref rootNode, e.Result);
            firstNode.Subcomponents.Add(rootNode);
            deviceTree.ItemsSource = firstNode.Subcomponents;
        }

        /*
         * Helper function: translate message from DeviceNode to Node
         */ 
        void LoadAllDevicesInfoHelper(ref Node node, MyDBService.DeviceNode dNode)
        {
            // ignore device node
            if (dNode.type == MyDBService.DeviceNodeType.Device)
            {
                return;
            }
            // only group nodes need
            for (int i = 0; i < dNode.children.Count; i++ )
            {
                Node cNode = new Node();
                cNode.NodeName = dNode.children[i].name;
                if (dNode.type == MyDBService.DeviceNodeType.Group)
                {
                    LoadAllDevicesInfoHelper(ref cNode, dNode.children[i]);
                }
                node.Subcomponents.Add(cNode);
            }
        }

    }
}
